package com.wenx.camel;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class AppWebService extends RouteBuilder {

    public static void main(String[] args) throws Exception {

        CamelContext context = new DefaultCamelContext(); // 1. 创建 CamelContext.
        context.addRoutes(new AppWebService());
        context.setTracing(true);
        context.start(); // 4. 启动CamelContext.
        Thread.sleep(Integer.MAX_VALUE);  // 为了保持CamelContext处于工作状态，这里需要sleep主线程
        context.stop(); // 最后停止CamelContext
    }

    /**
     * 生产者
     * @throws Exception
     */
    public void configure() throws Exception {
    	/**
        from("direct:start").process(new Processor() {
            public void process(Exchange exchange) throws Exception {
                exchange.getIn().setBody("Test Message from Camel Kafka Component Final",String.class);
                exchange.getIn().setHeader(KafkaConstants.PARTITION_KEY, 0);
                exchange.getIn().setHeader(KafkaConstants.KEY, "1");

                System.out.println(exchange.getOut());
            }
        }).to("kafka:10.10.5.212:9092?topic=test");  **/
    	
    	String content="<?xml version=\"1.0\" encoding=\"utf-8\"?>  <soapenv:Envelope"  
    	        + " xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"  
    	        + "<soapenv:Body><m:sayHell xmlns:m=\"http://service.wenx.com/\"><arg0>sfyang</arg0>"  
    	        + "</m:sayHell></soapenv:Body>"  
    	        + "</soapenv:Envelope>";
    	from("timer:foo?repeatCount=1")
        .setBody(constant(content))             //set request body
        .to("cxf:"
                + "http://localhost:8080/wx_service/WebService" //service address
                + "?"
                + "wsdlURL=http://localhost:8080/wx_service/WebService?wsdl"    //wsdl url
                + "&"
                + "dataFormat=RAW"        //dataformat type
        )
        .convertBodyTo(String.class)
        .to("stream:out");
    	
    	/**
    	from("timer:foo?repeatCount=1")  
        .process(new Processor() {      //use processor to set body  
            public void process(Exchange arg0) throws Exception {  
                WebServiceOperationRequest1 request1 = new WebServiceOperationRequest1();  
                request1.setIn("Hello1");  
                arg0.getIn().setBody(request1);  
            }  
        })  
        .to("cxf:"  
                + "http://localhost:8040/services/WebService" // service address  
                + "?"  
                + "wsdlURL=http://localhost:8040/services/WebService?wsdl" // wsdl url  
                + "&"  
                + "serviceClass=org.talend.esb.camel.cxf.WebServicePortType" //serviceClass  
                + "&" + "dataFormat=POJO" // dataformat type  
        ).to("log:output"); 
        **/ 
    }
}
